/*
Replication for:
Dynamics of Campaign Reporting and Press-Party Parallelism: 
Rise of Competitive Authoritarianism and the Media System in Turkey
by Yildirim, Baruh, Carkoglu
*/

* grc1leg: Combine graphs into one graph with a common legend by Wiggins
* BLINDSCHEMES: module to provide graph schemes sensitive to color vision deficiency by Bischof
* SYNTH: module to implement Synthetic Control Methods for Comparative Case Studies by Abadie, Diamond and Hainmuller for synt
* SUMSTATS: module to produce tables of summary statistics by Daniels
* ESTOUT: module to make regression tables by Jann
	*net install blindschemes_fix.pkg
	*net install grc1leg.pkg
	*net install synth.pkg
	*net install sumstats.pkg
	*net install estout.pkg

clear
graph drop _all

use "analyses.dta"

sort group date
tsset group date, daily

capture: mkdir figures
capture: mkdir tables

*Figure 1
preserve
collapse (mean) enp_pos enp_neg, by(year group)

graph twoway (connected enp_pos year if group==1, mcolor(black) msymbol(T) lcolor(black) lpattern(solid)) ///
	(connected enp_pos year if group==2, mcolor(black) msymbol(Sh) lcolor(black) lpattern(solid)) ///
	(connected enp_pos year if group==3, mcolor(black) msymbol(O) lcolor(black) lpattern(solid)), ///
	xtitle("Campaign Year") ytitle("ENP Positive") ylabel(1(0.25)2, glcolor(gs12)) xlabel(2002 2007 2011 2015, glcolor(gs12)) ///
	xmtick(2002 "2002" 2007 "2007" 2011 "2011" 2015 "2015") legend(order(1 "Conservative" 2 "Mainstream" 3 "Opposition") ///
	rows(1) position(6)) plotregion(fcolor(none) ifcolor(none)) name(pos, replace)

graph twoway (connected enp_neg year if group==1, mcolor(black) msymbol(T) lcolor(black) lpattern(solid)) ///
	(connected enp_neg year if group==2, mcolor(black) msymbol(Sh) lcolor(black) lpattern(solid)) ///
	(connected enp_neg year if group==3, mcolor(black) msymbol(O) lcolor(black) lpattern(solid)), ///
	xtitle("Campaign Year") ytitle("ENP Negative") ylabel(1(0.25)2, glcolor(gs12)) xlabel(2002 2007 2011 2015, glcolor(gs12)) ///
	xmtick(2002 "2002" 2007 "2007" 2011 "2011" 2015 "2015") legend(order(1 "Conservative" 2 "Mainstream" 3 "Opposition") ///
	rows(1) position(6)) plotregion(fcolor(none) ifcolor(none)) name(neg, replace)

grc1leg pos neg, name(figure1, replace)
graph export "figures/figure1.pdf", replace
graph drop pos neg
restore

*Figure 2
preserve
	*Replace favorability to range from -3 to +3
	collapse (mean)favorakp favorchp favormhp favorhdp , by(year group)
	foreach var of varlist favorakp favorchp favormhp favorhdp {
	replace `var'=`var'-4
	}
	
graph bar (asis) favorakp favorchp favormhp favorhdp, ///
	over(year, label(labsize(small) labcolor("black"))) ///
	over(group, relabel(1 "Conservative" 2 "Mainstream" 3 "Opposition") label(labcolor("black"))) ///
	bar(1, fcolor(black) lcolor(black) fin(100)) bar(2, fcolor(gs4) lcolor(gs4) fin(100)) ///
	bar(3, fcolor(gs8) lcolor(gs8) fin(100)) bar(4, fcolor(gs12) lcolor(gs12) fin(100)) ///
	legend(ring(0) region(fcolor(none)) order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") position(1) cols(2) ///
	rowgap(minuscule) bmargin(zero) colgap(minuscule) keygap(minuscule) size(small)) ///
	ytitle(Average Favorability) plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) name(figure2)

graph export "figures/figure2.pdf", replace
restore

*ENP, Table 2 and Figure 3:
reg enp_pos_ordinal l.enp_pos_ordinal i.group daystoelec i.year 
	eststo m1
reg enp_pos_ordinal l.enp_pos_ordinal i.group##c.daystoelec i.year 
	eststo m2

margins group, at(daystoelec=(0(10)90))
marginsplot, addplot(hist daystoelec, freq yaxis(2) yscale(alt axis(2)) below ///
	start(0) fcolor(%30) lcolor(none%0) xlabel(90(10)0, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
	lpattern(blank) legend(order(1 "Conservative" 2 "Mainstream" 3 "Opposition")) ytitle("", axis(2)) ylabel(1(0.2)2)) ///
	ytitle("") xtitle("Days to elections") xscale(reverse) ylabel(1(0.2)2) plotregion(fcolor(none) ifcolor(none)) ///
	title("Positive ENP") name(m2, replace) legend(rows(1))

reg enp_neg_ordinal l.enp_neg_ordinal i.group c.daystoelec i.year 
	eststo m3
reg enp_neg_ordinal l.enp_neg_ordinal i.group##c.daystoelec i.year 
	eststo m4

margins group, at(daystoelec=(0(10)90))
marginsplot, addplot(hist daystoelec, freq yaxis(2) yscale(alt axis(2)) below ///
	start(0) fcolor(%30) lcolor(none%0) xlabel(90(10)0, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
	lpattern(blank) legend(order(1 "Conservative" 2 "Mainstream" 3 "Opposition")) ytitle("", axis(2)) ylabel(1(0.2)2)) ///
	ytitle("") xtitle("Days to elections") xscale(reverse) ylabel(1(0.2)2) plotregion(fcolor(none) ifcolor(none)) ///
	title("Negative ENP") name(m4, replace) legend(rows(1))
	
	*Figure 3:
		grc1leg m2 m4, name(figure3, replace)
		graph export "figures/figure3.pdf", replace
		graph drop m2 m4
	
	*Table 2:
		esttab m1 m2 m3 m4 using "tables/table2.rtf", cells(b(star fmt(%9.3f)) se(par)) stats(r2_a N, fmt(%9.3f %9.0g) ///
		labels("R-Squared" "N")) legend label mtitles("Positive ENP" "Positive ENP" "Negative ENP" "Negative ENP") ///
		varlabels(_cons Constant L.enp_pos_ordinal "ENP Previous Day" L.enp_neg_ordinal "ENP Previous Day" ///
		2.group "Mainstream" 3.group "Opposition" daystoelec "Days to Election" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.group#c.daystoelec "Mainstream x Days to Elec." 3.group#c.daystoelec "Opposition x Days to Elec." ) /// 
		order(*.group *group#* daystoelec *.year L.*) ///
		drop(1.group 2002.year 1.group#c.daystoelec) replace collabels(none) nodepvars nonumbers ///
		addnotes("Linear regression, standard errors are in parentheses." ///
		"Reference categories: Conservative newspaper group and the 2002 election campaign.") ///
		title ("Table 2: Effective Number of Parties")

*ENP, Table S7 (Replicating models only with common days to election, 67 days to 2 days as in 2015):
	
	*There are different number of days across elections:
	table year, c(min daystoelec max daystoelec)

reg enp_pos_ordinal l.enp_pos_ordinal i.group daystoelec i.year if daystoelec<=66 & daystoelec>=2
eststo m1_si
reg enp_pos_ordinal l.enp_pos_ordinal i.group##c.daystoelec i.year if daystoelec<=66 & daystoelec>=2
eststo m2_si
reg enp_neg_ordinal l.enp_neg_ordinal i.group c.daystoelec i.year if daystoelec<=66 & daystoelec>=2
eststo m3_si
reg enp_neg_ordinal l.enp_neg_ordinal i.group##c.daystoelec i.year if daystoelec<=66 & daystoelec>=2
eststo m4_si

	*This sub-sample limits the sample to common days as a robustness check:
	table year if e(sample), c(min daystoelec max daystoelec)

	esttab m1_si m2_si m3_si m4_si using "tables/table_si_7.rtf", cells(b(star fmt(%9.3f)) se(par)) stats(r2_a N, fmt(%9.3f %9.0g) ///
		labels("R-Squared" "N")) legend label mtitles("Positive ENP" "Positive ENP" "Negative ENP" "Negative ENP") ///
		varlabels(_cons Constant L.enp_pos_ordinal "ENP Previous Day" L.enp_neg_ordinal "ENP Previous Day" ///
		2.group "Mainstream" 3.group "Opposition" daystoelec "Days to Election" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.group#c.daystoelec "Mainstream x Days to Elec." 3.group#c.daystoelec "Opposition x Days to Elec." ) /// 
		order(*.group *group#* daystoelec *.year L.*) ///
		drop(1.group 2002.year 1.group#c.daystoelec) replace collabels(none) nodepvars nonumbers ///
		addnotes("Linear regression, standard errors are in parentheses." ///
		"Reference categories: Conservative newspaper group and the 2002 election campaign.") ///
		title ("Table S7: Replicating ENP Model (Table 1) with 67 days:")

*Summary stats using the effective sample (1st part of Table S1 about the ENP)
reg enp_pos_ordinal l.enp_pos_ordinal i.group##c.daystoelec i.year 
	gen lagged_pos= l.enp_pos_ordinal if e(sample)
gen pos=1 if e(sample)

reg enp_neg_ordinal l.enp_neg_ordinal i.group##c.daystoelec i.year 
	gen lagged_neg= l.enp_neg_ordinal if e(sample)
gen neg=1 if e(sample)

	tab year, gen(yrs)
	tab group, gen(grp)
label variable lagged_pos "lagged enp pos"
label variable lagged_neg "lagged enp neg"
	
sumstats (enp_pos_ordinal lagged_pos daystoelec yrs* grp*  if pos==1) ///
		 (enp_neg_ordinal lagged_neg daystoelec yrs* grp*  if neg==1) ///
		 using "tables/table_si_1_1.xls", stats(n mean sd min max) replace

drop lagged_pos lagged_neg yrs* grp*
		
*Reshape to long form for party level analysis on favorability and visibility:
	reshape long favor voice, i(group date) j(part) string
	encode part, gen(party)
	drop part

*Rescale favorability to -3 to +3 range, change order of MHP and Kurdish parties:
	replace favor= favor-4
	recode party (4=3) (3=4)
	label define party 1 "AKP" 2"CHP" 3 "MHP" 4 "Kurdish Parties", replace

*Favorability, Table S3 and Figure 4:
reg favor l.favor i.group i.year c.daystoelec i.party
eststo m5
reg favor l.favor i.group i.year i.party##c.daystoelec
eststo m6

margins party, at(daystoelec=(0(10)90))
marginsplot, addplot(hist daystoelec, yaxis(2) yscale(alt axis(2)) below ///
	start(0) fcolor(%30) lcolor(none%0) xlabel(90(10)0, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
	lpattern(blank) legend(order(1 "AKP" 2 "CHP" 3 "MHP" "Kurdish Parties")) ytitle("", axis(2)) ylabel(-1(0.25)1)) ///
	ytitle("Favorability") xtitle("Days to elections") xscale(reverse) plotregion(fcolor(none) ifcolor(none)) ///
	title("Panel A - Within Campaign") name(fig4_1, replace) legend(rows(1))
	
reg favor l.favor i.group i.party##i.year c.daystoelec 
eststo m7

margins year#party
	marginsplot, xdimension(year) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel B - Across Campaigns") ylabel(-1(0.25)1) xtitle("Election Year") ytitle("Favorability") name(fig4_2, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12))

reg favor l.favor i.party##i.group i.year c.daystoelec 
eststo m8

	*Figure 4:
		grc1leg fig4_1 fig4_2, legendfrom(fig4_2) name(figure4)
		graph export "figures/figure4.pdf", replace
		graph drop fig4_1 fig4_2
	
	*Table S3:
		esttab m5 m6 m7 m8 using "tables/table_si_3.rtf", cells(b(star fmt(%9.3f)) se(par)) stats(r2_a N, fmt(%9.3f %9.0g) ///
		labels("R-Squared" "N")) legend label mtitles("(1)" "(2)" "(3)" "(4)") ///
		varlabels(_cons Constant L.favor "Lagged Favorability" 2.party "CHP" 3.party "MHP" 4.party "Kurdish Parties" ///
		2.group "Mainstream" 3.group "Opposition" daystoelec "Days to Election" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.party#c.daystoelec "CHP x Days to Elec." 3.party#c.daystoelec "MHP x Days to Elec." ///
		4.party#c.daystoelec "Kurdish Parties x Days to Elec." 2.party#2007.year "CHP X 2007" 2.party#2011.year "CHP X 2011" /// 
		2.party#2015.year "CHP X 2015" 3.party#2007.year "MHP x 2007" 3.party#2011.year "MHP X 2011" 3.party#2015.year "MHP X 2015" ///
		4.party#2007.year "Kurdish Parties x 2007" 4.party#2011.year "Kurdish Parties X 2011" 4.party#2015.year "Kurdish Parties X 2015" ///
		2.party#2.group "CHP X Mainstream" 2.party#3.group "CHP X Opposition" ///
		3.party#2.group "MHP X Mainstream" 3.party#3.group "MHP X Opposition" ///
		4.party#2.group "Kurdish Parties X Mainstream" 4.party#3.group "Kurdish Parties X Opposition" ) /// 
		order(L.favor 2.group 3.group 2007.year 2011.year 2015.year daystoelec 2.party 3.party 4.party ///
		2.party#c.daystoelec 3.party#c.daystoelec 4.party#c.daystoelec *#2007.year *#2011.year *#2015.year *#2.group *#3.group) ///
		drop(1.group 1.group 2002.year 1.party 1.party#c.daystoelec 1.party#* *.party#2002.year *#1.group) replace collabels(none) nodepvars nonumbers ///
		addnotes("Standard errors in parentheses" ///
				 "OLS Models, estimating favorability given to a specific party by a newspaper group on a given day during a campaign period." /// 
				 "Reference categories are 2002 election campaign, conservative newspaper group and the incumbent party (AKP)." ///
				 "Reference categories for interaction models are the same: AKP, 2002 campaign, and conservative media group.") ///
		title ("Table S3, Favorability:")

*Favorability, Table S8 and Figure S4 (Replicating models only with common days to election, 67 days to 2 days as in 2015):		
reg favor l.favor i.group i.year c.daystoelec i.party if daystoelec<=66 & daystoelec>=2
eststo m5_2
reg favor l.favor i.group i.year i.party##c.daystoelec if daystoelec<=66 & daystoelec>=2
eststo m6_2

margins party, at(daystoelec=(0(10)90))
marginsplot, addplot(hist daystoelec, yaxis(2) yscale(alt axis(2)) below ///
	start(0) fcolor(%30) lcolor(none%0) xlabel(90(10)0, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
	lpattern(blank) legend(order(1 "AKP" 2 "CHP" 3 "MHP" "Kurdish Parties")) ytitle("", axis(2)) ylabel(-1(0.25)1)) ///
	ytitle("Favorability") xtitle("Days to elections") xscale(reverse) plotregion(fcolor(none) ifcolor(none)) ///
	title("Panel A - Within Campaign") name(figs4_1, replace) legend(rows(1))
	
reg favor l.favor i.group i.party##i.year c.daystoelec if daystoelec<=66 & daystoelec>=2
eststo m7_2

margins year#party
	marginsplot, xdimension(year) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel B - Across Campaigns") ylabel(-1(0.25)1) xtitle("Election Year") ytitle("Favorability") name(figs4_2, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12))

reg favor l.favor i.party##i.group i.year c.daystoelec if daystoelec<=66 & daystoelec>=2
eststo m8_2

	*Figure S4:
		grc1leg figs4_1 figs4_2, legendfrom(figs4_2) name(figure_si_4)
		graph export "figures/figure_si_4.pdf", replace
		graph drop figs4_1 figs4_2

	*Table S8:
		esttab m5_2 m6_2 m7_2 m8_2 using "tables/table_si_8.rtf", cells(b(star fmt(%9.3f)) se(par)) stats(r2_a N, fmt(%9.3f %9.0g) ///
		labels("R-Squared" "N")) legend label mtitles("(1)" "(2)" "(3)" "(4)") ///
		varlabels(_cons Constant L.favor "Lagged Favorability" 2.party "CHP" 3.party "MHP" 4.party "Kurdish Parties" ///
		2.group "Mainstream" 3.group "Opposition" daystoelec "Days to Election" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.party#c.daystoelec "CHP x Days to Elec." 3.party#c.daystoelec "MHP x Days to Elec." ///
		4.party#c.daystoelec "Kurdish Parties x Days to Elec." 2.party#2007.year "CHP X 2007" 2.party#2011.year "CHP X 2011" /// 
		2.party#2015.year "CHP X 2015" 3.party#2007.year "MHP x 2007" 3.party#2011.year "MHP X 2011" 3.party#2015.year "MHP X 2015" ///
		4.party#2007.year "Kurdish Parties x 2007" 4.party#2011.year "Kurdish Parties X 2011" 4.party#2015.year "Kurdish Parties X 2015" ///
		2.party#2.group "CHP X Mainstream" 2.party#3.group "CHP X Opposition" ///
		3.party#2.group "MHP X Mainstream" 3.party#3.group "MHP X Opposition" ///
		4.party#2.group "Kurdish Parties X Mainstream" 4.party#3.group "Kurdish Parties X Opposition" ) /// 
		order(L.favor 2.group 3.group 2007.year 2011.year 2015.year daystoelec 2.party 3.party 4.party ///
		2.party#c.daystoelec 3.party#c.daystoelec 4.party#c.daystoelec *#2007.year *#2011.year *#2015.year *#2.group *#3.group) ///
		drop(1.group 1.group 2002.year 1.party 1.party#c.daystoelec 1.party#* *.party#2002.year *#1.group) replace collabels(none) nodepvars nonumbers ///
		addnotes("Standard errors in parentheses" ///
				 "OLS Models, estimating favorability given to a specific party by a newspaper group on a given day during a campaign period." /// 
				 "Reference categories are 2002 election campaign, conservative newspaper group and the incumbent party (AKP)." ///
				 "Reference categories for interaction models are the same: AKP, 2002 campaign, and conservative media group.") ///
		title ("Table S8, Replicating Favorability Model (Table S3) with 67 days:")

*Voice, Table S4 and Figure 5:
reg voice l.voice i.group i.year c.daystoelec i.party
eststo m8

reg voice l.voice i.group i.year i.party##c.daystoelec
eststo m9

margins party, at(daystoelec=(0(10)90))
marginsplot, addplot(hist daystoelec, yaxis(2) yscale(alt axis(2)) below ///
	start(0) fcolor(%30) lcolor(none%0) xlabel(90(10)0, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
	lpattern(blank) legend(order(1 "AKP" 2 "CHP" 3 "MHP" "Kurdish Parties")) ytitle("", axis(2)) ylabel(0(0.1)0.5)) ///
	ytitle("Visibility") xtitle("Days to elections") xscale(reverse) plotregion(fcolor(none) ifcolor(none)) ///
	title("Panel A - Within Campaign") name(fig5_1, replace) legend(rows(1))
	
reg voice l.voice i.group i.party##i.year c.daystoelec
eststo m10

margins year#party
	marginsplot, xdimension(year) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel B - Across Campaigns") ylabel(0(0.1)0.5) xtitle("Election Year") ytitle("Visibility") name(fig5_2, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12))

reg voice l.voice i.party##i.group i.year c.daystoelec
eststo m11

	*Figure 5:
		grc1leg fig5_1 fig5_2, legendfrom(fig5_2) name(figure5)
		graph export "figures/figure5.pdf", replace
		graph drop fig5_1 fig5_2
	
	*Table S4:
		esttab m8 m9 m10 m11 using "tables/table_si_4.rtf", cells(b(star fmt(%9.3f)) se(par)) stats(r2_a N, fmt(%9.3f %9.0g) ///
		labels("R-Squared" "N")) legend label mtitles("(1)" "(2)" "(3)" "(4)") ///
		varlabels(_cons Constant L.voice "Lagged Visibility" 2.party "CHP" 3.party "MHP" 4.party "Kurdish Parties" ///
		2.group "Mainstream" 3.group "Opposition" daystoelec "Days to Election" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.party#c.daystoelec "CHP x Days to Elec." 3.party#c.daystoelec "MHP x Days to Elec." ///
		4.party#c.daystoelec "Kurdish Parties x Days to Elec." 2.party#2007.year "CHP X 2007" 2.party#2011.year "CHP X 2011" /// 
		2.party#2015.year "CHP X 2015" 3.party#2007.year "MHP x 2007" 3.party#2011.year "MHP X 2011" 3.party#2015.year "MHP X 2015" ///
		4.party#2007.year "Kurdish Parties x 2007" 4.party#2011.year "Kurdish Parties X 2011" 4.party#2015.year "Kurdish Parties X 2015" ///
		2.party#2.group "CHP X Mainstream" 2.party#3.group "CHP X Opposition" ///
		3.party#2.group "MHP X Mainstream" 3.party#3.group "MHP X Opposition" ///
		4.party#2.group "Kurdish Parties X Mainstream" 4.party#3.group "Kurdish Parties X Opposition" ) /// 
		order(L.voice 2.group 3.group 2007.year 2011.year 2015.year daystoelec 2.party 3.party 4.party ///
		2.party#c.daystoelec 3.party#c.daystoelec 4.party#c.daystoelec *#2007.year *#2011.year *#2015.year *#2.group *#3.group) ///
		drop(1.group 1.group 2002.year 1.party 1.party#c.daystoelec 1.party#* *.party#2002.year *#1.group) ///
		replace collabels(none) nodepvars nonumbers ///
		addnotes("Standard errors in parentheses" ///
				 "OLS Models, estimating visibility given to a specific party by a newspaper group on a given day during a campaign period." /// 
				 "Reference categories are 2002 election campaign, conservative newspaper group and the incumbent party (AKP)." ///
				 "Reference categories for interaction models are the same: AKP, 2002 campaign, and conservative media group.") ///
		title ("Table S4, Visibility of Relevant Parties")


*Voice, Table S9 and Figure S5 (Replicating models only with common days to election, 67 days to 2 days as in 2015):		
reg voice l.voice i.group i.year c.daystoelec i.party if daystoelec<=66 & daystoelec>=2
eststo m8_2

reg voice l.voice i.group i.year i.party##c.daystoelec if daystoelec<=66 & daystoelec>=2
eststo m9_2

margins party, at(daystoelec=(0(10)90))
marginsplot, addplot(hist daystoelec, yaxis(2) yscale(alt axis(2)) below ///
	start(0) fcolor(%30) lcolor(none%0) xlabel(90(10)0, glcolor(gs12)) ylabel(, glcolor(gs12)) ///
	lpattern(blank) legend(order(1 "AKP" 2 "CHP" 3 "MHP" "Kurdish Parties")) ytitle("", axis(2)) ylabel(0(0.1)0.5)) ///
	ytitle("Visibility") xtitle("Days to elections") xscale(reverse) plotregion(fcolor(none) ifcolor(none)) ///
	title("Panel A - Within Campaign") name(fig5_1, replace) legend(rows(1))
	
reg voice l.voice i.group i.party##i.year c.daystoelec if daystoelec<=66 & daystoelec>=2
eststo m10_2

margins year#party
	marginsplot, xdimension(year) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel B - Across Campaigns") ylabel(0(0.1)0.5) xtitle("Election Year") ytitle("Visibility") name(fig5_2, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12))

reg voice l.voice i.party##i.group i.year c.daystoelec if daystoelec<=66 & daystoelec>=2
eststo m11_2

	*Figure S5:
		grc1leg fig5_1 fig5_2, legendfrom(fig5_2) name(figure_si_5)
		graph export "figures/figure_si_5.pdf", replace
		graph drop fig5_1 fig5_2
		
	*Table S9:
		esttab m8_2 m9_2 m10_2 m11_2 using "tables/table_si_9.rtf", cells(b(star fmt(%9.3f)) se(par)) stats(r2_a N, fmt(%9.3f %9.0g) ///
		labels("R-Squared" "N")) legend label mtitles("(1)" "(2)" "(3)" "(4)") ///
		varlabels(_cons Constant L.voice "Lagged Visibility" 2.party "CHP" 3.party "MHP" 4.party "Kurdish Parties" ///
		2.group "Mainstream" 3.group "Opposition" daystoelec "Days to Election" 2007.year "2007" 2011.year "2011" ///
		2015.year "2015" 2.party#c.daystoelec "CHP x Days to Elec." 3.party#c.daystoelec "MHP x Days to Elec." ///
		4.party#c.daystoelec "Kurdish Parties x Days to Elec." 2.party#2007.year "CHP X 2007" 2.party#2011.year "CHP X 2011" /// 
		2.party#2015.year "CHP X 2015" 3.party#2007.year "MHP x 2007" 3.party#2011.year "MHP X 2011" 3.party#2015.year "MHP X 2015" ///
		4.party#2007.year "Kurdish Parties x 2007" 4.party#2011.year "Kurdish Parties X 2011" 4.party#2015.year "Kurdish Parties X 2015" ///
		2.party#2.group "CHP X Mainstream" 2.party#3.group "CHP X Opposition" ///
		3.party#2.group "MHP X Mainstream" 3.party#3.group "MHP X Opposition" ///
		4.party#2.group "Kurdish Parties X Mainstream" 4.party#3.group "Kurdish Parties X Opposition" ) /// 
		order(L.voice 2.group 3.group 2007.year 2011.year 2015.year daystoelec 2.party 3.party 4.party ///
		2.party#c.daystoelec 3.party#c.daystoelec 4.party#c.daystoelec *#2007.year *#2011.year *#2015.year *#2.group *#3.group) ///
		drop(1.group 1.group 2002.year 1.party 1.party#c.daystoelec 1.party#* *.party#2002.year *#1.group) ///
		replace collabels(none) nodepvars nonumbers ///
		addnotes("Standard errors in parentheses" ///
				 "OLS Models, estimating visibility given to a specific party by a newspaper group on a given day during a campaign period." /// 
				 "Reference categories are 2002 election campaign, conservative newspaper group and the incumbent party (AKP)." ///
				 "Reference categories for interaction models are the same: AKP, 2002 campaign, and conservative media group.") ///
		title ("Table S9, Visibility of Relevant Parties")

*Figure 6 and Figure S6 Visibility and Favorability across Newspaper Groups:

	*Figure 6:
	reg voice l.voice i.party##i.group i.year c.daystoelec
	margins party#group
	marginsplot, xdimension(group) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel A - Visibility") ylabel(0(0.1)0.5) ytitle("") xtitle("Election Year") name(fig6_1, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12) labsize(small))

	reg favor l.favor i.party##i.group i.year c.daystoelec
	margins party#group
	marginsplot, xdimension(group) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel B - Favorability") ylabel(-1(0.5)1) ytitle("") xtitle("Election Year") name(fig6_2, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12) labsize(small))

	grc1leg fig6_1 fig6_2, name(figure6, replace)
	graph export "figures/figure6.pdf", replace
	graph drop fig6_1 fig6_2

	*Figure S6:
	reg voice l.voice i.party##i.group i.year c.daystoelec if daystoelec<=66 & daystoelec>=2
	margins party#group
	marginsplot, xdimension(group) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel A - Visibility") ylabel(0(0.1)0.5) ytitle("") xtitle("Election Year") name(figs6_1, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12) labsize(small))

	reg favor l.favor i.party##i.group i.year c.daystoelec if daystoelec<=66 & daystoelec>=2
	margins party#group
	marginsplot, xdimension(group) legend(order(1 "AKP" 2 "CHP" 3 "MHP" 4 "Kurdish Parties") pos(6) row(1)) ///
	title("Panel B - Favorability") ylabel(-1(0.5)1) ytitle("") xtitle("Election Year") name(figs6_2, replace) ///
	plotregion(fcolor(none) ifcolor(none)) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12) labsize(small))

	grc1leg figs6_1 figs6_2, name(figure_si_6, replace)
	graph export "figures/figure_si_6.pdf", replace
	graph drop figs6_1 figs6_2

*Summary stats using the effective sample (2nd part of Table S1 on Favorability and Visibility)
reg favor l.favor i.party i.group i.year c.daystoelec
	gen lagged_fav= l.favor if e(sample)
	gen fav=1 if e(sample)
reg voice l.voice i.party##i.group i.year c.daystoelec
	gen lagged_voice= l.voice if e(sample)
	gen vis=1 if e(sample)

tab year, gen(yrs)
tab group, gen(grp)
tab party, gen(prt)
label variable lagged_fav "Lagged favorability"
label variable lagged_voice "Lagged visibility"
	
sumstats (favor lagged_fav daystoelec grp* prt* yrs*   if fav==1) ///
		 (voice lagged_voice daystoelec grp* prt* yrs*  if vis==1) ///
		 using "tables/table_si_1_2.xls", stats(n mean sd min max) replace

drop lagged_fav lagged_voice yrs* grp* prt*
	
	
clear	
	
/*
---------------------------------------------------------------------------------------------------
Factor Analysis to Groups Newspapers:
---------------------------------------------------------------------------------------------------
*/

	*Table 6 for the first factor used to group newspapers into three:
	use "groups.dta"
	
	*Create new observations for the table row on % variance explained of the factor analysis:
		insobs 4
		replace news = "% explained" in 55
		replace news = "% explained" in 56
		replace news = "% explained" in 57
		replace news = "% explained" in 58
		replace year = 2002 in 55
		replace year = 2007 in 56
		replace year = 2011 in 57
		replace year = 2015 in 58
		
	gen factor=.
	
	foreach num of numlist 2002 2007 2011 2015{
		factor akp chp mhp kurdish if year==`num'
		rotate
		predict f1`num' if year==`num'
		replace factor= f1`num' if year==`num'
		drop f1`num'
		matrix define temp = e(r_Ev)
		mata : st_matrix("sum", rowsum(st_matrix("temp")))
		replace factor= (temp[1,1]/sum[1,1]) if year==`num' & news == "% explained"
	}
	
	*Fix the ranges for all four years to run in the same direction, pro-akp negative, opposition positive values:
		replace factor=-factor if year==2011 in 1/8
		replace factor=-factor if year==2015 in 1/8
	
	*Resahpe to table format across years:
		keep year news factor
		reshape wide factor, i(news) j(year)
	
	*Table s6 values:
		edit
		export delimited using "tables/table_si_6.txt", delimiter(tab) replace

clear
		
/*
---------------------------------------------------------------------------------------------------
Analysis of Circulation Shares (Supplementary Information):
---------------------------------------------------------------------------------------------------
*/

use "circulation.dta"

*Figure S1 and Table S5:
	preserve
	keep if changed==1

*Figure S1:
	gen perc100= percentage*100
	by weeks, sort: egen average_before= mean(perc100) if newspaper!="ZAMAN" & weeks<0
	by weeks, sort: egen average_after= mean(perc100) if newspaper!="ZAMAN" & weeks>0
	
graph twoway (lowess perc100 weeks if weeks<0 & weeks>-53 & newspaper!="ZAMAN", lcolor(plr1) lwidth(thick)) ///
	(lowess perc100 weeks if weeks>0 & weeks<53 & newspaper!="ZAMAN", lcolor(plb1) lwidth(thick)) ///
	(line average_after weeks if week>-53 & weeks<53, lcolor(black) lwidth(thin)) ///
	(line average_before weeks if week>-53 & weeks<53, lcolor(black) lwidth(thin)) if newspaper!="ZAMAN", ///
	xtitle("Weeks before and after sale") ytitle("") ylabel(5(0.2)6, glcolor(gs12)) xlabel(-50 (25) 50, glcolor(gs12)) ///
	legend(order(1 "Before Sale" 2 "After Sale") rows(1) position(6)) plotregion(fcolor(none) ifcolor(none)) name(figure_si_1, replace)
	graph export "figures/figure_si_1.pdf", replace
	drop perc100 average_before average_after
	
*Table S5:
	gen percentage2= percentage if weeks>0 & weeks<=52
	gen percentage1= percentage if weeks<0 & weeks>=-52
	collapse (mean) percentage1 percentage2 if newspaper!="Zaman", by(newspaper)
	gen diff= percentage2-percentage1
	insobs 1
	replace newspaper="TOTAL" in 9
	egen total_1= sum(percentage1)
	egen total_2= sum(percentage2)
	gen total_diff= total_2-total_1
	replace percentage1= total_1 in 9
	replace percentage2= total_2 in 9
	replace diff= total_diff in 9
	drop total_1 total_2 total_diff
	foreach var of varlist percentage1 percentage2 diff {
	replace `var'= `var'*100
	}
	format percentage1 percentage2 diff %9.3g
	export delimited using "tables/table_si_5.txt", delimiter(tab) replace
	
restore

*Figures S2 and S3 - Synthetic Control

list newspaper date_week if weeks==0
	*This is the list for which we will create synthetic controls across newspapers - except Zaman:
	*Note that Milliyet and Vatan & Sabah and Takvim were sold at the same time

*For each newspaper, we will drop papers that were not fully available during the sale period
*And for each paper, we will keep only the 52 weeks before & after sale
*Then calculate the gap and save as new dataset by the sold paper's name, eventually we will append all these datasets together:

*cd to a new subfolder to reduce clutter:
	capture: mkdir synth_data
	local dir `c(pwd)'
	cd "`dir'/synth_data"
	
*Aksam newspaper:
	preserve
	keep if date_week> 378 & date_week < 496
	*note, some weeks before sale are missing (12 weeks), so we compensate for those with other ones.
	drop if newspaper=="SOL GAZETESİ"
	drop if newspaper=="FOTOGOL ADAM"
	drop if newspaper=="MİLAT"
	drop if newspaper=="DAILY SABAH"
	synth percentage thisweek lastweek percentage, trunit(1) trperiod(443) keep(aksam) replace
	restore

*Milliyet and Vatan newspapers (sold at the same time):
	preserve
	keep if date_week> 281 & date_week < 387
	drop if newspaper=="EFSANE FOTOSPOR"
	drop if newspaper=="T.DAILY NEWS"
	drop if newspaper=="AYDINLIK"
	drop if newspaper=="ANADOLUDA VAKİT"
	drop if newspaper=="HÜRRİYET DAILY NEWS"
	drop if newspaper=="REFERANS"
	drop if newspaper=="YURT"
	drop if newspaper=="HALKA VE OLAYLARA TERCÜMAN"
	synth percentage thisweek lastweek percentage, trunit(37) trperiod(334) keep(milliyet) replace 
	synth percentage thisweek lastweek percentage, trunit(57) trperiod(334) keep(vatan) replace 
	restore

*Sabah and Takvim (sold at the same time):
	preserve
	keep if date_week> 69 & date_week < 175
	drop if newspaper=="T.DAILY NEWS"
	drop if newspaper=="TODAY'S ZAMAN"
	drop if newspaper=="GÖZCÜ"
	drop if newspaper=="SÖZCÜ"
	drop if newspaper=="OKEY"
	drop if newspaper=="YENİ ASIR"
	drop if newspaper=="EVET"
	drop if newspaper=="FORUM DİPLOMATİK"
	drop if newspaper=="VAPIO"
	drop if newspaper=="TARAF"
	drop if newspaper=="YENİGÜN"
	drop if newspaper=="OLAY"
	drop if newspaper=="YARIŞ GAZETESİ"
	drop if newspaper=="DOKUZ SÜTUN"
	drop if newspaper=="ÖZGÜR GÜNDEM"
	drop if newspaper=="FOTOGOL ADAM"
	synth percentage thisweek lastweek percentage, trunit(45) trperiod(122) keep(sabah) replace 
	synth percentage thisweek lastweek percentage, trunit(51) trperiod(122) keep(takvim) replace 
	restore

*Hurriyet and Posta:
	preserve
	keep if date_week>642 & date_week <748
	by newspaper, sort: gen check=_N
	keep if check==105
	list date_week if weeks==0
	synth percentage thisweek lastweek percentage, trunit(28) trperiod(695) keep(hurriyet) replace 
	synth percentage thisweek lastweek percentage, trunit(42) trperiod(695) keep(posta) replace
	clear

*Combine datasets:
	use "aksam.dta"
	gen newspaper="AKŞAM"
	append using "milliyet.dta"
	replace newspaper="MİLLİYET" if newspaper==""
	append using "sabah.dta"
	replace newspaper="SABAH" if newspaper==""
	append using "takvim.dta"
	replace newspaper="TAKVİM" if newspaper==""
	append using "vatan.dta"
	replace newspaper="VATAN" if newspaper==""
	append using "hurriyet.dta"
	replace newspaper="HÜRRİYET" if newspaper==""
	append using "posta.dta"
	replace newspaper="POSTA" if newspaper==""
	drop _Co_Number _W_Weight

*Gap between the observed and synthetic values:
gen gap= _Y_treated-_Y_synthetic

sort newspaper _time

*Fix time to range the same for all newspapers:
	replace _time= _time-443 if newspaper=="AKŞAM"
	replace _time=_n-53 if _time<0 & newspaper=="AKŞAM"
	replace _time= _time-122 if newspaper=="SABAH"
	replace _time= _time-334 if newspaper=="MİLLİYET"
	replace _time= _time-122 if newspaper=="TAKVİM"
	replace _time= _time-334 if newspaper=="VATAN"
	replace _time= _time-695 if newspaper=="HÜRRİYET"
	replace _time= _time-695 if newspaper=="POSTA"

reshape wide gap _Y_treated _Y_synthetic, i(_time) j(newspaper) string

*Calculate the average gap between observed and synthetic:
	egen meangap= rowmean(gapAKŞAM gapMİLLİYET gapSABAH gapTAKVİM gapVATAN gapHÜRRİYET gapPOSTA)
	replace meangap=meangap*100

*Back to working directory from synth_data clutter to save figures:
cd ..

*Figure S2:
	replace _Y_treatedVATAN= _Y_treatedVATAN*100
	replace _Y_syntheticVATAN = _Y_syntheticVATAN*100
	replace gapVATAN= gapVATAN*100

	line _Y_treatedVATAN _time, sort lcolor(plr1) || line _Y_syntheticVATAN _time, ///
		sort lcolor(plb1) legend(pos(6) cols(2) order(1 "Vatan" 2 "Synthetic Vatan")) xtitle("Weeks before and after sale") ///
		ytitle("Circulation Share") ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12)) ///
		plotregion(fcolor(none) ifcolor(none)) name(a, replace)
	line gapVATAN _time, sort ytitle("Gap between Vatan and Synthetic Vatan") xtitle("Weeks before and after sale") ///
		name(b, replace) legend(order(1 "Gap") on pos(6)) lcolor(black) ylabel(, glcolor(gs12)) xlabel(, glcolor(gs12)) ///
		plotregion(fcolor(none) ifcolor(none))

	graph combine a b, name(figure_si_2, replace)
	graph export "figures/figure_si_2.pdf", replace
	graph drop a b
	
*Figure S3:
	line meangap _time, sort xtitle("Weeks before and after sale") ytitle("Average gap for 7 newspapers") ///
	ylabel(-1(0.5).5, glcolor(gs12)) xlabel(-50 (50) 50, glcolor(gs12)) ///
	plotregion(fcolor(none) ifcolor(none)) name(figure_si_3, replace)
	graph export "figures/figure_si_3.pdf", replace
